﻿using LogLib;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;

namespace PrintExcel
{
    public class ExcelData
    {
        private string _error = "";
        public string ErrorMessage
        {
            get { return _error; }
        }

        public string AssemblyDirectory
        {
            get
            {
                string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
                directory = directory.Substring(6);
                return directory;
            }
        }

        private PaperPrinterOption _printer = new PaperPrinterOption();
        public PaperPrinterOption Printer
        {
            get { return _printer; }
        }

        private Dictionary<string, RangeItemInfo> mapItems = new Dictionary<string, RangeItemInfo>();
        public Dictionary<string, RangeItemInfo> Items
        {
            get { return mapItems; }
        }

        public bool ContainsItem(string itemName)
        {
            return mapItems.ContainsKey(itemName);
        }

        public RangeItemInfo this[string itemName]
        {
            get
            {
                if(mapItems.ContainsKey(itemName))
                {
                    return mapItems[itemName];
                }
                else
                {
                    throw new Exception("关键字不在字典中:" + itemName);
                }
            }
        }

        public ExcelData()
        {
           
        }

        public bool LoadConfig()
        {
            mapItems.Clear();
            try
            {                
                string xmlDataFile = Path.Combine(AssemblyDirectory, "config", "ExcelData.xml");
                XDocument xmlDoc = XDocument.Load(xmlDataFile);
                XElement root = xmlDoc.Element("Layout");
                if(root == null)
                {
                    _error = "没有找到配置文件的根节点.";
                    Log.WriteAsync(_error);
                    return false;
                }                
                LoadPrinterInfo(root);              
                LoadBarCode(root);
                LoadTextItem(root);
                LoadCheckItem(root);
                LoadUnderLineItem(root);
                LoadPictureItem(root);
                LoadPackageImageItem(root);
                return true;
            }
            catch (Exception e)
            {
                _error = "加载Excel模板数据配置文件发生了错误," + e.Message;
                Log.WriteException(e.ToString());
                return false;
            }          
        }

          
        private void LoadPrinterInfo(XElement root)
        {
            if(!File.Exists(_printer.ConfigFilePath))
            {
                _printer.Save();
            }
            _printer.Load();
        }

        /// <summary>
        /// 加载条形码单元格对象
        /// </summary>
        private void LoadBarCode(XElement element)
        {
            XElement parentElem = element.Element("BarCode");
            if(parentElem == null)
            {
                return;
            }
            foreach(XElement e in parentElem.Elements())
            {
                RangeItemInfo info = new RangeItemInfo();
                info.Type = ItemType.BarCode;
                info.Name = e.Name.LocalName;
                info.OffsetX = Convert.ToSingle(e.Attribute("OffsetX").Value);
                info.OffsetY = Convert.ToSingle(e.Attribute("OffsetY").Value);
                info.Height = Convert.ToInt32(e.Attribute("Height").Value);
                info.Width = Convert.ToInt32(e.Attribute("Width").Value);
                info.Start = e.Attribute("TopLeft").Value;
                info.End = e.Attribute("BottomRight").Value;
                if (mapItems.ContainsKey(info.Name))
                {
                    Log.WriteAsync("键值已存在:" + info.Name);
                }
                else
                {
                    mapItems.Add(info.Name, info);
                }
            }
        }

        /// <summary>
        /// 加载文本单元格对象
        /// </summary>
        private void LoadTextItem(XElement element)
        {
            XElement parentElem = element.Element("Text");
            if (parentElem == null)
            {
                return;
            }
            foreach (XElement e in parentElem.Elements())
            {
                RangeItemInfo info = new RangeItemInfo();
                info.Type = ItemType.Text;
                info.Name = e.Name.LocalName;             
                info.Start = e.Attribute("TopLeft").Value;
                info.End = e.Attribute("BottomRight").Value;
                if (mapItems.ContainsKey(info.Name))
                {
                    Log.WriteAsync("键值已存在:" + info.Name);
                }
                else
                {
                    mapItems.Add(info.Name, info);
                }
            }
        }


        /// <summary>
        /// 加载条形码单元格对象
        /// </summary>
        private void LoadCheckItem(XElement root)
        {
            XElement parentElem = root.Element("Check");
            if (parentElem == null)
            {
                return;
            }
            foreach (XElement e in parentElem.Elements())
            {
                RangeItemInfo info = new RangeItemInfo();
                info.Type = ItemType.CheckBox;
                info.Name = e.Name.LocalName;
                info.Start = e.Attribute("TopLeft").Value;
                info.End = e.Attribute("BottomRight").Value;
                if (mapItems.ContainsKey(info.Name))
                {
                    Log.WriteAsync("键值已存在:" + info.Name);
                }
                else
                {
                    mapItems.Add(info.Name, info);
                }
            }
        }

        private void LoadUnderLineItem(XElement root)
        {
            XElement parentElem = root.Element("UnderLine");
            if (parentElem == null)
            {
                return;
            }
            foreach (XElement e in parentElem.Elements())
            {
                RangeItemInfo info = new RangeItemInfo();
                info.Type = ItemType.UndeLine;
                info.Name = e.Name.LocalName;
                info.Start = e.Attribute("TopLeft").Value;
                info.End = e.Attribute("BottomRight").Value;
                if (mapItems.ContainsKey(info.Name))
                {
                    Log.WriteAsync("键值已存在:" + info.Name);
                }
                else
                {
                    mapItems.Add(info.Name, info);
                }
            }
        }

        private void LoadPictureItem(XElement root)
        {
            XElement parentElem = root.Element("Picture");
            if (parentElem == null)
            {
                return;
            }
            foreach (XElement e in parentElem.Elements())
            {
                RangeItemInfo info = new RangeItemInfo();
                info.Type = ItemType.Picture;
                info.Name = e.Name.LocalName;
                info.OffsetX = Convert.ToSingle(e.Attribute("OffsetX").Value);
                info.OffsetY = Convert.ToSingle(e.Attribute("OffsetY").Value);              
                info.Start = e.Attribute("TopLeft").Value;
                info.End = e.Attribute("BottomRight").Value;
                if(e.Attribute("WidthScal") != null)
                {
                    info.Scale = Convert.ToInt32(e.Attribute("WidthScal").Value);
                }
                if (info.Scale == 0F)
                {
                    info.Scale = 1.0F;
                }
                if (mapItems.ContainsKey(info.Name))
                {
                    Log.WriteAsync("键值已存在:" + info.Name);
                }
                else
                {
                    mapItems.Add(info.Name, info);
                }
            }
        }

        private void LoadPackageImageItem(XElement root)
        {
            XElement parentElem = root.Element("PackageImage");
            if (parentElem == null)
            {
                return;
            }
            foreach (XElement e in parentElem.Elements())
            {
                RangeItemInfo info = new RangeItemInfo();
                info.Type = ItemType.Picture;
                info.Name = e.Name.LocalName;
                info.OffsetX = Convert.ToSingle(e.Attribute("OffsetX").Value);
                info.OffsetY = Convert.ToSingle(e.Attribute("OffsetY").Value);
                info.Start = e.Attribute("TopLeft").Value;
                info.End = e.Attribute("BottomRight").Value;
                if(e.Attribute("Side") != null)
                {
                    info.Side = e.Attribute("BottomRight").Value;
                }              
                if (string.IsNullOrEmpty(info.Side))
                {
                    info.Side = "front";
                }
                if (mapItems.ContainsKey(info.Name))
                {
                    Log.WriteAsync("键值已存在:" + info.Name);
                }
                else
                {
                    mapItems.Add(info.Name, info);
                }
            }
        }
    }
}
